using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Game;
public class FirstController : MonoBehaviour, SceneController, UsersOperation{
    readonly Vector3 waterPos = new Vector3(0,0.5F,0);
    UserGUI userGUI;
    public coastControllor fromCoast;
    public coastControllor toCoast;
    public boatControllor boat;
    public figureControllor[] figures;
    public Lock synLock;
    void Awake(){
        Director director = Director.getInstance();
        synLock = Lock.getInstance();
        director.currentSceneController = this;
        userGUI = gameObject.AddComponent <UserGUI>() as UserGUI;
        figures = new figureControllor[6];
        loadResources();
    }
    public void loadResources() {
        GameObject water = Instantiate(Resources.Load ("Prefabs/Water", typeof(GameObject)), waterPos, Quaternion.identity, null) as GameObject;
        water.name = "water";
        fromCoast = new coastControllor("from");
        toCoast = new coastControllor("to");
        boat = new boatControllor();
        loadFigures();
    }
    private void loadFigures() {
        for (int i = 0 ; i < 3; i++) {
            figureControllor newF = new figureControllor("priest");
            newF.setName("priest"+i);
            newF.setPos(fromCoast.getEmptyPosition());
            newF.moveToCoast(fromCoast);
            fromCoast.moveToCoast(newF);
            figures[i] = newF;
        }
        for (int i = 0 ; i < 3; i++) {
            figureControllor newF = new figureControllor("devil");
            newF.setName("devil"+i);
            newF.setPos(fromCoast.getEmptyPosition());
            newF.moveToCoast(fromCoast);
            fromCoast.moveToCoast(newF);
            figures[i+3] = newF;
        }
    }
    public void moveBoat() {
        if (synLock.getLock() == 0) return;
        synLock.setLock(0);
        if (boat.isEmpty())  return;
        if (userGUI.status != 0) return;
            boat.Move();
            userGUI.status = check_game_over();
            
        
    }
    public void figureClicked(figureControllor figureCon) {
        if (synLock.getLock() == 0) return;
        synLock.setLock(0);
        if (userGUI.status != 0 ) return;
        if (figureCon.IsOnBoat()) {
            coastControllor coast;
            coast = (boat.getDirection()==-1)?toCoast:fromCoast;
            boat.GetOffBoat(figureCon.getName());
            figureCon.moveToPos(coast.getEmptyPosition());
            figureCon.moveToCoast(coast);
            coast.moveToCoast(figureCon);
        } else {
            coastControllor coast = figureCon.getCoastControllor();
            if (boat.getEmptyIndex() == -1) return;
            if (coast.getDirection() != boat.getDirection()) return;
            coast.getOffCoast(figureCon.getName());
            figureCon.moveToPos(boat.getEmptyPos());
            figureCon.moveToBoat(boat);
            boat.moveToBoat(figureCon); 
        }
        userGUI.status = check_game_over();
        
    }
    int check_game_over() {
        int from_priest = 0;
        int to_priest = 0;
        int from_devil = 0;
        int to_devil = 0;
        int[] fromCount = fromCoast.getFigureNum();
        from_priest += fromCount[0];
        from_devil += fromCount[1];
        int[] toCount = toCoast.getFigureNum();
        to_priest += toCount[0];
        to_devil += toCount[1];
        if (to_devil+to_priest == 6) {
            return 2;
        }
        int[] boatCount = boat.getFigureNum();
        if (boat.getDirection() == -1) {
            to_priest+=boatCount[0];
            to_devil+=boatCount[1];
        } else {
            from_priest+=boatCount[0];
            from_devil+=boatCount[1];
        }
        if (from_priest < from_devil && from_priest > 0) return 1;
        if (to_devil > to_priest && to_priest > 0) return 1;
        return 0;
    }
    public void restart() {
        boat.reset();
        toCoast.reset();
        fromCoast.reset();
        for (int i = 0; i < figures.Length; i++) {
            figures[i].reset();
        }
    }
}